package com.cheng.chengliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.cheng.chengliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.cheng.chengliving.commodity.entity.AttrEntity;
import com.cheng.chengliving.commodity.service.AttrAttrgroupRelationService;
import com.cheng.chengliving.commodity.service.AttrService;
import com.cheng.chengliving.commodity.service.CategoryService;
import com.cheng.chengliving.commodity.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cheng.chengliving.commodity.entity.AttrgroupEntity;
import com.cheng.chengliving.commodity.service.AttrgroupService;
import com.cheng.common.utils.PageUtils;
import com.cheng.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author zhucheng
 * @email 1920361386@qq.com
 * @date 2022-11-23 11:23:21
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {
    @Autowired
    private AttrgroupService attrgroupService;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取某个分类的所有属性组及其关联的基本属性
     */
    @RequestMapping("{catalogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catalogId") Long categoryId) {
        List<AttrGroupWithAttrsVo> data =
                attrgroupService.getAttrGroupWithAttrsByCategoryId(categoryId);

        return R.ok().put("data",data);
    }

    /**
     * 批量添加基本属性和属性组关联关系
     */
    @RequestMapping("/attr/relation")
    public R attrAddRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities){
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntities);
        return R.ok();
    }

    /**
     * 根据attrgroupId返回可以关联的基本属性
     */
    @RequestMapping("/{attrGroupId}/attr/allowrelation")
    public R attrAllowRelation(@PathVariable("attrGroupId") Long attrGroupId,
                               @RequestParam Map<String,Object> params) {
        PageUtils page = attrService.getAllowRelationAttr(params, attrGroupId);
        return R.ok().put("page",page);
    }

    /**
     * 响应批量删除属性组和基本属性的关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities) {

        attrService.deleteRelation(attrAttrgroupRelationEntities);
        return R.ok();
    }


    /**
     * 根据groupId属性组id返回该属性组关联的基本属性
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {

        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",attrEntities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrgroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 这里是方法的重载
     * 1.根据业务需求,增加根据分类+查询条件+分页的API接口
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                             @PathVariable("categoryId") Long categoryId){

        PageUtils page = attrgroupService.queryPage(params,categoryId);

        return R.ok().put("page", page);
    }

    //装配CategoryService
    @Resource
    private CategoryService categoryService;
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrgroupEntity attrgroup = attrgroupService.getById(id);

		//获取到该属性组对应的categoryId
        Long categoryId = attrgroup.getCategoryId();
        //获取到与categoryId相关的id组[1,101,1001]
        Long[] cascadeCategoryId = categoryService.getCascadeCategoryId(categoryId);
        //把cascadeCategoryId设置到attrgroup对象的cascadeCategoryId属性上
        attrgroup.setCascadeCategoryId(cascadeCategoryId);

        return R.ok().put("attrgroup", attrgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
